package com.trainoo.payment.mock.controller;

import com.trainoo.payment.mock.config.CallbackUrl;
import com.trainoo.payment.mock.config.Config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * <p> add description here </p>
 *
 * @author tao.zhou
 * @version 1.0
 * @date 2023/6/6 15:22:00
 */
@RestController
public class ConfigController {

    private static final Logger logger = LoggerFactory.getLogger(PaymentController.class);

    @Resource
    private Config config;

    @RequestMapping(value = "/config/show", produces = "text/html;charset=UTF-8")
    public String getConfig() {
        Boolean autoCallback = config.getAutoCallback();
        StringBuilder sb = new StringBuilder();
        sb.append("</br> ### 支付成功结果自动回调通知配置（不自动回调则需要扫码触发回调通知）:\t\n</br> ");
        sb.append("- 是否自动回调成功: ").append(autoCallback).append("\t\n</br> ");
        sb.append("- 自动回调延迟时间: ").append(config.getSleepTime()/1000).append("s\t\n</br> ");
        sb.append("- 不自动回调列表: ").append(config.getBlackList()).append("\t\n</br> ");
        sb.append("- 接口返回是否延迟: ").append(config.getDelay()).append("\t\n</br> ");
        sb.append("- 接口返回延迟时间: ").append(config.getDelayTime()/1000).append("s\t\n</br> ");
        sb.append("\t\n");
        sb.append("</br> ### 回调地址配置 (优先使用接口传入的[notifyUrl]字段作为回调地址，不传则在以下列表寻找):\t\n</br> ");
        HashMap<String, String> urlMap = CallbackUrl.getUrlMap();
        for (String ip : urlMap.keySet()) {
            sb.append("- ").append(ip).append(" > ").append(urlMap.get(ip)).append("\t\n</br> ");
        }
        sb.append("\t\n");
        sb.append("</br> ### 修改配置:\t\n</br> ");
        sb.append("- [GET|POST]/config/show: 查看配置信息\t\n</br> ");
        sb.append("- [GET|POST]/config/changeAuto: 切换是否自动回调\t\n</br> ");
        sb.append("- [GET|POST]/config/changeAuto/{time}: 设置自动回调延迟时间(单位：ms)\t\n</br> ");
        sb.append("- [GET|POST]/config/delay: 切换是否增加接口延迟\t\n</br> ");
        sb.append("- [GET|POST]/config/delay/{time}: 设置接口返回延迟时间(单位：ms)\t\n</br> ");
        sb.append("- [POST]/config/setUrl: 新增回调地址（参数：{\"ip\":\"请求来源IP\",\"url\":\"回调地址\"}）\t\n</br> ");
        sb.append("- [POST]/config/delUrl: 删除回调地址（参数：{\"ip\":\"请求来源IP\"}）\t\n</br> ");
        sb.append("- [POST]/config/setBlackList: 设置不自动回调IP列表（参数：{\"blackList\":\"ip1,ip2,ip3\"}）\t\n</br> ");
        sb.append("\t\n</br> End.");
        return sb.toString();
    }

    @RequestMapping(value = "/config/changeAuto")
    public String changeAuto() {
        Boolean autoCallback = config.getAutoCallback();
        config.setAutoCallback(!autoCallback);
        logger.info("更新配置项[autoCallback], 更新后的值为：{}", config.getAutoCallback());
        return getConfig();
    }

    @RequestMapping(value = "/config/changeAuto/{time}")
    public String changeAutoTime(@PathVariable(value = "time") Long time) {
        config.setSleepTime(time);
        logger.info("更新配置项[sleepTime], 更新后的值为：{}", config.getSleepTime());
        return getConfig();
    }

    @RequestMapping(value = "/config/delay")
    public String setDelay() {
        Boolean delay = config.getDelay();
        config.setDelay(!delay);
        logger.info("更新配置项[delay], 更新后的值为：{}", config.getDelay());
        return getConfig();
    }

    @RequestMapping(value = "/config/delay/{time}")
    public String setDelayTime(@PathVariable(value = "time") Long time) {
        config.setDelayTime(time);
        logger.info("更新配置项[delayTime], 更新后的值为：{}", config.getDelayTime());
        return getConfig();
    }

    @PostMapping(value = "/config/setUrl")
    public String setUrl(String ip, String url) {
        CallbackUrl.setUrl(ip, url);
        return getConfig();
    }

    @PostMapping(value = "/config/delUrl")
    public String delUrl(String ip) {
        CallbackUrl.delUrl(ip);
        return getConfig();
    }

    @PostMapping(value = "/config/setBlackList")
    public String setBlackList(String blackList) {
        config.setBlackList(blackList);
        return getConfig();
    }
}
